package com.changfeng.service.impl;

import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.ReatailersAddress;
import com.changfeng.bean.ReatailersCommodity;
import com.changfeng.bean.ReatailersOrderCommodityMerge;
import com.changfeng.bean.RetailersOrder;
import com.changfeng.bean.dto.vo.CartToCommodityVO;
import com.changfeng.bean.dto.vo.PayOrderVO;
import com.changfeng.common.CurrentUser;
import com.changfeng.dao.ReatailersAddressMapper;
import com.changfeng.dao.ReatailersCommodityMapper;
import com.changfeng.dao.ReatailersOrderCommodityMergeMapper;
import com.changfeng.dao.RetailersOrderMapper;
import com.changfeng.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RetailersOrderMapper retailersOrderMapper;

    @Autowired
    private ReatailersOrderCommodityMergeMapper reatailersOrderCommodityMergeMapper;

    @Autowired
    private ReatailersCommodityMapper reatailersCommodityMapper;

    @Autowired
    private ReatailersAddressMapper reatailersAddressMapper;
    @Override
    public Integer addOrder(List<CartToCommodityVO> vo, Integer addressId) {
        List<ReatailersCommodity> commoditys = new ArrayList();
        List<ReatailersOrderCommodityMerge> merges = new ArrayList<>();
        double total = 0.0d;
        for (CartToCommodityVO cart : vo) {
            ReatailersCommodity reatailersCommodity = reatailersCommodityMapper.selectByPrimaryKey(cart.getCommodityId());
            total = total + reatailersCommodity.getCommodityPrice().doubleValue() * cart.getCommodityNum();
            commoditys.add(reatailersCommodity);
            ReatailersOrderCommodityMerge merge = new ReatailersOrderCommodityMerge();
            merge.setCommodityId(reatailersCommodity.getCommodityId());
            merge.setCommodityNumber(cart.getCommodityNum());
            merge.setGmtCreate(new Date());
            merge.setGmtModified(new Date());
            merges.add(merge);
        }
        ReatailersAddress reatailersAddress = reatailersAddressMapper.selectByPrimaryKey(addressId);
        UserAuth currentUser = CurrentUser.getCurrentUser();
        RetailersOrder retailersOrder = new RetailersOrder();
        retailersOrder.setUserId(currentUser.getUserId());
        retailersOrder.setOrderCommodityid(vo.get(0).getCommodityId());
        retailersOrder.setOrderPrice(new BigDecimal(total));
        retailersOrder.setOrderState("wfk");
        retailersOrder.setUserAddress(reatailersAddress.getAddressDescribe());
        retailersOrder.setGmtCreate(new Date());
        retailersOrder.setGmtModified(new Date());
        retailersOrderMapper.insert(retailersOrder);

        for (ReatailersOrderCommodityMerge merge : merges) {
            merge.setOrderId(retailersOrder.getOrderId());
            reatailersOrderCommodityMergeMapper.insert(merge);
        }

        return retailersOrder.getOrderId();
    }

    @Override
    public PayOrderVO getPayOrder(Integer id) {
        RetailersOrder retailersOrder = retailersOrderMapper.selectByPrimaryKey(id);
        ReatailersCommodity reatailersCommodity = reatailersCommodityMapper.selectByPrimaryKey(retailersOrder.getOrderCommodityid());
        PayOrderVO vo = new PayOrderVO();
        vo.setOrderId(retailersOrder.getOrderId());
        vo.setOrderPrice(retailersOrder.getOrderPrice().doubleValue());
        vo.setUserAddress(retailersOrder.getUserAddress());
        vo.setOrderImg(reatailersCommodity.getCommodityImgurl());
        return vo;
    }

}
